<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page
	import="com.google.appengine.api.datastore.EntityNotFoundException"%>
<%@page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@page import="com.google.appengine.api.datastore.Key"%>
<%@page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%  
	DatastoreService d = DatastoreServiceFactory.getDatastoreService();
	Key Skey = KeyFactory.createKey("Students", "Students");
	Query Sq = new Query("Student", Skey);
	List<Entity> Sl = d.prepare(Sq).asList(FetchOptions.Builder.withDefaults());
	
	
	ArrayList<List<Entity>> k = new ArrayList<List<Entity>>(); 
	ArrayList<Entity> Sl2 = new ArrayList<Entity>();
	ArrayList<int[]> MV = new ArrayList<int[]>();
		
	
	Sl2.clear();
	Sl2.add(Sl.get(0));
	k.add(Sl2);	
	
	String cc=  (String)Sl.get(0).getProperty("classCode");
	int kk = 0 ;
	int v = 0 ;
	int m = 0 ;
	for(int i = 1 ; i< Sl.size() ; i++){
		if(((String)Sl.get(i).getProperty("sex")).compareTo("M")==0){
			m++;
		}else{
			v++;
		}
		
		//		//System.out.println((String)Sl.get(i).getProperty("classCode"));
	 
	if (cc.compareTo((String)Sl.get(i).getProperty("classCode"))==0){
		k.get(kk).add(Sl.get(i));
		//System.out.println((String)Sl.get(i).getProperty("classCode"));
	}else{
		kk++;
		cc=  (String)Sl.get(i).getProperty("classCode");
		Sl2=  new ArrayList<Entity>();
		Sl2.add(Sl.get(i));
		k.add(Sl2);
		//System.out.println("---------------k size "+k.size()+" class "+(String)k.get(kk).get(0).getProperty("classCode")+" aantal"+ k.get(kk).size());
		int[] x = new int[2];
		x[0] = v;
		x[1] = m;
		MV.add(x);
		v = 0;
		m = 0;
		
	}

	}
	int[] x = new int[2];
	x[0] = v;
	x[1] = m;
	MV.add(x);
	v = 0;
	m = 0;
	
	
		
	%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="/include/style/style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Klassen gegevens</title>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'klas');
        data.addColumn('number', 'aantal leerlingen');
        data.addRows([
       
      <%
        for (int i = 0 ; i  < k.size() ; i++){          	        	
        	if(i == k.size()-1){
        		out.println("['"+k.get(i).get(0).getProperty("classCode")+"',"+k.get(i).size()+"]");
                	}else{out.println("['"+k.get(i).get(0).getProperty("classCode")+"',"+k.get(i).size()+"],");}
        	}
        %>        	
          ]);

        var options = {
          width: 800, height: 600,
          title: '<%=k.size()%> klassen, aantal leerlingen per klas' 

		};

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
	}
</script>


<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
	google.load("visualization", "1", {
		packages : [ "corechart" ]
	});
	google.setOnLoadCallback(drawChart);
	function drawChart() {
		var data = new google.visualization.DataTable();
		data.addColumn('string', 'klass');
		data.addColumn('number', 'aantal leerlingen');
		data.addColumn('number', 'aantal meisjes');
		data.addColumn('number', 'aantal jongens');
		
		data.addRows([
		<%

			int jongens = 0;
			int meisjes = 0;

           for (int i = 0 ; i  < k.size() ; i++){         
           	
           	if(i == k.size()-1){
           		out.println("['"+k.get(i).get(0).getProperty("classCode")+"',"+k.get(i).size()+","+MV.get(i)[0]+","+MV.get(i)[1]+"]");
                  	}else{out.println("['"+k.get(i).get(0).getProperty("classCode")+"',"+k.get(i).size()+","+MV.get(i)[0]+","+MV.get(i)[1]+"],");}
           
           	jongens=jongens+MV.get(i)[1];
           	meisjes=meisjes+MV.get(i)[0];
           }
         %>
	]);

		var options = {
				 width: 800, height: 3000,
			title : 'Aantal leerlingen :<%=Sl.size()%> meisjes : <%=meisjes%>  jongens : <%=jongens%> ',
			vAxis : {
				title : 'aantal leerlingen',
				titleTextStyle : {
					color : 'red'
				}
			}
		};

		var chart = new google.visualization.BarChart(document
				.getElementById('chart_div1'));
		chart.draw(data, options);
	}
</script>
</head>
<body>
<jsp:include page="include/header.jsp" />
<br>
	<a href="javascript:history.go(-1)">&larr;Ga terug naar student/vak selectie</a>
	<h2>Klassen gegevens</h2>
	<div id="chart_div"></div>
	<p style="clear: both;" />
	<h2>Verhouding jongens : meisjes</h2>
	<div id="chart_div1"></div>
</body>
</html>